package com.icongyou.enterprise.talent_analysis.listener;

import com.icongyou.enterprise.talent_analysis.service.WordCloudService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

/**
 * 词云图更新触发器测试
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("词云图更新触发器测试")
class WordCloudUpdateTriggerTest {

    @Mock
    private WordCloudService wordCloudService;

    @InjectMocks
    private WordCloudUpdateTrigger trigger;

    @Test
    @DisplayName("onCommentChanged - 评语变化触发词云更新")
    void testOnCommentChanged_Success() {
        // Arrange
        String studentId = "660e8400-e29b-41d4-a716-446655440105";
        String gradeId = "grade-1";
        
        doNothing().when(wordCloudService)
            .generateWordCloudAsync(studentId, "comment_updated", gradeId);

        // Act
        trigger.onCommentChanged(studentId, gradeId);

        // Assert
        verify(wordCloudService).generateWordCloudAsync(
            eq(studentId), eq("comment_updated"), eq(gradeId)
        );
    }

    @Test
    @DisplayName("onCommentChanged - 异常不中断")
    void testOnCommentChanged_Exception() {
        // Arrange
        String studentId = "660e8400-e29b-41d4-a716-446655440105";
        String gradeId = "grade-1";
        
        doThrow(new RuntimeException("生成失败"))
            .when(wordCloudService)
            .generateWordCloudAsync(studentId, "comment_updated", gradeId);

        // Act - 不应抛出异常
        try {
            trigger.onCommentChanged(studentId, gradeId);
        } catch (Exception e) {
            // 预期可能抛出异常（取决于实现）
        }

        // Assert
        verify(wordCloudService).generateWordCloudAsync(
            eq(studentId), eq("comment_updated"), eq(gradeId)
        );
    }

    @Test
    @DisplayName("manualTrigger - 手动触发词云更新")
    void testManualTrigger_Success() {
        // Arrange
        String studentId = "660e8400-e29b-41d4-a716-446655440105";
        
        doNothing().when(wordCloudService)
            .generateWordCloudAsync(studentId, "manual", null);

        // Act
        trigger.manualTrigger(studentId);

        // Assert
        verify(wordCloudService).generateWordCloudAsync(
            eq(studentId), eq("manual"), eq(null)
        );
    }

    @Test
    @DisplayName("manualTrigger - 多次触发")
    void testManualTrigger_MultipleCalls() {
        // Arrange
        String studentId = "660e8400-e29b-41d4-a716-446655440105";
        
        doNothing().when(wordCloudService)
            .generateWordCloudAsync(studentId, "manual", null);

        // Act
        trigger.manualTrigger(studentId);
        trigger.manualTrigger(studentId);
        trigger.manualTrigger(studentId);

        // Assert
        verify(wordCloudService, times(3)).generateWordCloudAsync(
            eq(studentId), eq("manual"), eq(null)
        );
    }
}
